﻿using System;
using System.Text;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Ddl;
using Me.Prettyprint.Hector.Api.Exceptions;

namespace Me.Prettyprint.Cassandra.Serializers
{
    public class TypeInferringSerializer<T> : AbstractSerializer<T>
    {
        public static TypeInferringSerializer<T> Instance = new TypeInferringSerializer<T>();
        /// <summary>
        /// Call GetComparatorType&lt;T>(T) instead to get the correct ComparatorType
        /// </summary>
        /// <returns></returns>
        public override ComparatorType GetComparatorType()
        {
            throw new HectorException("The type inferring serializer can't return the correct ComparatorType due to how Generics works in C#. This doesn't translate well from Java to C#.");
        }
        /// <summary>
        /// This is a work around for GetComparatorType() so that AbstractComposite can use it
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string GetComparatorType<T>(T obj)
        {
            return SerializerTypeInferer.GetSerializer<T>(obj).GetComparatorType().TypeName;
        }
        public override byte[] ToBytes(T obj)
        {
            return SerializerTypeInferer.GetSerializer<T>(obj).ToBytes(obj);
        }
        public override Utils.ByteBuffer ToByteBuffer(T obj)
        {
            return new ByteBuffer(ToBytes(obj));
        }
        public override T FromBytes(byte[] byteBuffer)
        {
            throw new HectorException("The type inferring serializer can only be used for data going to the database, and not data coming from the database");
        }
        public override T FromByteBuffer(ByteBuffer buff)
        {
            throw new HectorException("The type inferring serializer can only be used for data going to the database, and not data coming from the database");
        }
    }
}
